<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>注册</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }
        
        body {
            width: 100%;
            height: 100%;
            background: url(img.jpg) no-repeat 0 0;
            background-size: cover;
        }
        
        #register {
            padding: 20px 40px;
            width: 260px;
            position: absolute;
            left: 50%;
            top: 50%;
            transform: translate(-50%, -50%);
            background-color: rgba(255, 255, 255, .8);
            line-height: 20px;
            border-radius: 20px;
        }
        
        #register #userName {
            margin-bottom: 5px;
            margin-top: 7px;
        }
        
        #register .password {
            margin-bottom: 5px;
        }
        
        #register #code {
            margin-left: 16px;
            outline: none;
            width: 162px;
        }
        
        #register #btn {
            float: right;
            margin: 10px;
        }
        
        button {
            width: 50px;
        }
        
        input {
            outline: none;
        }
        
        h3 {
            margin-bottom: 5px;
        }
        
        #rentHolidy {
            font-size: 13px;
            margin-top: 5px;
        }
        
        #rentHolidy #data {
            margin-top: 3px;
        }
        
        #name {
            margin-left: -15px;
        }
        
        #password {
            margin-left: 17px;
        }
    </style>
</head>

<body>
    <div id="register">
        <h3>注册</h3>
        <hr>
        <div id="userName">
            <span>用户名：</span>
            <input type="text" placeholder="请输入用户名" id="username">
        </div>
        <div class="password">
            <span>真实姓名：</span>
            <input type="text" placeholder="请输入真实姓名" id="name">
        </div>
        <div class="password">
            <span>密码：</span>
            <input type="password" placeholder="请输入密码" id="password">
        </div>
        <div id="userSex">
            <input type="radio" name="sex" id="isteacher">老师
            <input type="radio" name="sex" checked>学生
        </div>
        <div id="btn">
            <button id="enroll"> 注册</button>
        </div>
    </div>
    <script src="lib/jquery.min.js"></script>
    <script>
        let address = "http://10.35.170.159:82"
            // let address = "http://192.168.1.102:82";
        $("#enroll").click(function() {
            let username = $("#username").val();
            let password = $("#password").val();
            let name = $("#name").val();
            if (username && password && name) {
                console.log($("#isteacher")[0].checked ? 0 : 1)
                $.ajax({
                    url: `${address}/addUser`,
                    data: {
                        username: $("#username").val(),
                        password: $("#password").val(),
                        name: $("#name").val(),
                        usertype: $("#isteacher")[0].checked ? 0 : 1,
                    },
                    success(data) {
                        if (data === "error") {
                            alert("注册失败")
                        } else {
                            alert("注册成功")
                            location.href = `${address}/register.html`
                        }
                    }

                })
            }

        })
    </script>
</body>

</html>